package com.qiaofang.collectmanage.stub.feign.client.impl;
import java.util.List;

import com.qiaofang.collectmanage.stub.bean.*;
import com.qiaofang.collectmanage.stub.request.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qiaofang.common.constant.CommonResponseCode;
import com.qiaofang.common.exception.BusinessException;
import com.qiaofang.common.model.page.PageDTO;
import com.qiaofang.collectmanage.stub.response.SearchCrawlerTaskResponse;
import com.qiaofang.collectmanage.stub.response.*;
import com.qiaofang.collectmanage.stub.facade.CrawlerTaskFacade;
import com.qiaofang.collectmanage.stub.feign.client.CrawlerTaskClient;
import com.qiaofang.collectmanage.stub.response.ListCrawlerTaskResponse;
import com.qiaofang.collectmanage.stub.response.RemoveCrawlerTaskResponse;
import com.qiaofang.collectmanage.stub.response.SaveCrawlerTaskResponse;
import com.qiaofang.collectmanage.stub.response.UpdateCrawlerTaskResponse;
import com.qiaofang.collectmanage.stub.request.GetCrawlerTaskByTaskRequest;
import com.qiaofang.collectmanage.stub.response.GetCrawlerTaskByTaskResponse;

import com.qiaofang.collectmanage.stub.response.GetCrawlerTaskByUuidResponse;
import com.qiaofang.collectmanage.stub.response.ListCrawlerTaskByUuidsResponse;

/**
* Created by generator on 2018-8-1 13:49:21
*/
@Service("taskClient")
public class CrawlerTaskClientImpl implements CrawlerTaskClient {

    @Autowired
    private CrawlerTaskFacade taskFacade;

    @Override
    public CrawlerTaskDTO getTaskByUuid(String uuid){
        GetCrawlerTaskByUuidRequest request = new GetCrawlerTaskByUuidRequest();
        request.setUuid(uuid);
        GetCrawlerTaskByUuidResponse response = taskFacade.getCrawlerTaskByUuid(request);
        if (CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
             return response.getTaskDTO();
        } else {
            throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
    }
  @Override
   public CrawlerTaskDTO getTaskByTask(CrawlerTaskDTO dto){
        GetCrawlerTaskByTaskRequest request = new GetCrawlerTaskByTaskRequest();
        request.setTaskDTO(dto);
        GetCrawlerTaskByTaskResponse response = taskFacade.getCrawlerTaskByTask(request);
        if (CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
            return response.getTaskDTO();
        } else {
           throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
   }

   @Override
    public void saveTask(SaveCrawlerTaskDTO dto) {
        SaveCrawlerTaskRequest request = new SaveCrawlerTaskRequest();
        request.setTaskDTO(dto);
        SaveCrawlerTaskResponse response = taskFacade.saveCrawlerTask(request);
        if (!CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
            throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
    }

    @Override
    public void updateTask (UpdateCrawlerTaskDTO dto) {
        UpdateCrawlerTaskRequest request = new UpdateCrawlerTaskRequest();
        request.setTaskDTO(dto);
        UpdateCrawlerTaskResponse response = taskFacade.updateCrawlerTask(request);
        if (!CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
           throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
    }
    @Override
    public void removeTaskByUuid (String uuid) {
        RemoveCrawlerTaskRequest request = new RemoveCrawlerTaskRequest();
        request.setUuid(uuid);
        RemoveCrawlerTaskResponse response = taskFacade.removeCrawlerTask(request);
        if (!CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
           throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
    }
    @Override
    public List<CrawlerTaskDTO> listTask(ListCrawlerTaskDTO dto){
        ListCrawlerTaskRequest request = new ListCrawlerTaskRequest();
        request.setTaskDTO(dto);
        ListCrawlerTaskResponse response = taskFacade.listCrawlerTask(request);
        if (CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
           return response.getList();
        } else {
           throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
    }
        @Override
        public List<CrawlerTaskDTO> listTaskByUuids(List<String> uuidList){
            ListCrawlerTaskByUuidsRequest request = new ListCrawlerTaskByUuidsRequest();
            request.setTaskUuidList(uuidList);
            ListCrawlerTaskByUuidsResponse response = taskFacade.listCrawlerTaskByUuids(request);
            if (CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
            return response.getList();
            } else {
            throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
            }
       }
    @Override
    public SearchCrawlerTaskResultDTO searchTask(PageDTO pageDTO, SearchCrawlerTaskDTO dto){
        SearchCrawlerTaskRequest request = new SearchCrawlerTaskRequest();
        request.setSearchTaskDTO(dto);
        request.setPageDTO(pageDTO);
        SearchCrawlerTaskResponse response = taskFacade.searchCrawlerTask(request);
        if (CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
               SearchCrawlerTaskResultDTO result = new SearchCrawlerTaskResultDTO();
               result.setList(response.getList());
               result.setCount(response.getCount());
               return result;
        } else {
            throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
     }


    @Override
    public List<CrawlerTaskRelatedDTO> listCrawlerTaskCollectRuleByIds(List<Integer> ids) {
        ListCollectRuleByIdsRequest listCollectRuleByIdsRequest = new ListCollectRuleByIdsRequest();
        listCollectRuleByIdsRequest.setIds(ids);
        ListCollectRuleByIdsResponse response = taskFacade.listCrawlerTaskCollectRuleByIds(listCollectRuleByIdsRequest);
        return response.getData();
    }

}
